import delimited "C:\Output_Updated.csv", delimiter(comma)

global controls shareofelderly65 shareofmalepopulation year
global healthcontrols averagehealth smokingprevalencein bmi30
global ginigdp gini gdppc gdppc2 
global education averageeducationlevel

encode country, gen(cntry)
xtset cntry year

*Descriptive statistics of measures of health equality
summarize cowellup cowelldown conindex if year==2002
summarize cowellup cowelldown conindex if year==2010
summarize cowellup cowelldown conindex if year==2018

asdoc summarize cowellup cowelldown conindex if year==2002, stat(mean sd), replace
asdoc summarize cowellup cowelldown conindex if year==2010, stat(mean sd), append
asdoc summarize cowellup cowelldown conindex if year==2018, stat(mean sd), append

*Descriptive statistics of average health
asdoc summarize averagehealth if year==2002, stat(mean sd), replace
asdoc summarize averagehealth if year==2010, stat(mean sd), append
asdoc summarize averagehealth if year==2018, stat(mean sd), append

*Effect of democracy for above median countries
reg cowelldown democracytime $controls if democracytime>=57, cluster(country)
outreg2 using cowelldown_OLS_abovemedian.doc, replace e(r2 r2_a)

reg cowelldown democracytime $controls pca if democracytime>=57, cluster(country)
outreg2 using cowelldown_OLS_abovemedian.doc, append e(r2 r2_a)

reg cowelldown democracytime $controls pca $ginigdp if democracytime>=57, cluster(country)
outreg2 using cowelldown_OLS_abovemedian.doc, append e(r2 r2_a)

reg cowellup democracytime $controls if democracytime>=57, cluster(country)
outreg2 using cowellup_OLS_abovemedian.doc, replace e(r2 r2_a)

reg cowellup democracytime $controls pca if democracytime>=57, cluster(country)
outreg2 using cowellup_OLS_abovemedian.doc, append e(r2 r2_a)

reg cowellup democracytime $controls pca $ginigdp if democracytime>=57, cluster(country)
outreg2 using cowellup_OLS_abovemedian.doc, append e(r2 r2_a)

reg conindex democracytime $controls if democracytime>=57, cluster(country)
outreg2 using coindex_OLS_abovemedian.doc, replace e(r2 r2_a)

reg conindex democracytime $controls pca if democracytime>=57, cluster(country)
outreg2 using coindex_OLS_abovemedian.doc, append e(r2 r2_a)

reg conindex democracytime $controls pca $ginigdp if democracytime>=57, cluster(country)
outreg2 using coindex_OLS_abovemedian.doc, append e(r2 r2_a)

*OLS for average health
reg averagehealth democracytime $controls, cluster(country)
outreg2 using averagehealth_OLS.doc, replace e(r2 r2_a)

reg averagehealth democracytime $controls pca, cluster(country)
outreg2 using averagehealth_OLS.doc, append e(r2 r2_a)

reg averagehealth democracytime $controls pca $ginigdp, cluster(country)
outreg2 using averagehealth_OLS.doc, append e(r2 r2_a)

*IV for average health
ivreg averagehealth (democracytime=laggeddemocracytime15) $controls, cluster(country)
outreg2 using averagehealth_IVREG.doc, replace e(r2 r2_a)

ivreg averagehealth (democracytime=laggeddemocracytime15) $controls pca, cluster(country)
outreg2 using averagehealth_IVREG.doc, append e(r2 r2_a)

ivreg averagehealth (democracytime=laggeddemocracytime15) $controls pca $ginigdp, cluster(country)
outreg2 using averagehealth_IVREG.doc, append e(r2 r2_a)

*OLS for education
reg averageeducationlevel democracytime $controls, cluster(country)
outreg2 using education_OLS.doc, replace e(r2 r2_a)

reg averageeducationlevel democracytime $controls pca, cluster(country)
outreg2 using education_OLS.doc, append e(r2 r2_a)

reg averageeducationlevel democracytime $controls pca $ginigdp, cluster(country)
outreg2 using education_OLS.doc, append e(r2 r2_a)

*IV for education
ivreg averageeducationlevel (democracytime=laggeddemocracytime15) $controls, cluster(country)
outreg2 using education_IVREG.doc, replace e(r2 r2_a)

ivreg averageeducationlevel (democracytime=laggeddemocracytime15) $controls pca, cluster(country)
outreg2 using education_IVREG.doc, append e(r2 r2_a)

ivreg averageeducationlevel (democracytime=laggeddemocracytime15) $controls pca $ginigdp, cluster(country)
outreg2 using education_IVREG.doc, append e(r2 r2_a)
